/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.managed;

import java.io.Serializable;
import java.util.*;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import ru.ssau.datamanager.DataManager;
import ru.ssau.entities.DataAccessException;
import ru.ssau.entities.User;

@Named("user")
@SessionScoped
public class UserBean implements Serializable {

    public static final String ADD = "Add";
    public static final String EDIT = "Edit";
    public static final String NO_USER_TO_EDIT = "noUserToEdit";
    public static final String VIEW = "View";

    @EJB
    private DataManager dataManager;

    private String searchParameter;

    private Integer id;
    private String name;
    private String info;
    private String login;
    private String password;
    //reg date
    private Integer year;
    private Integer month;
    private Integer day;
    private Integer hours;
    private Integer minutes;
    private Integer seconds;

    private Integer selectedUserId;
    private User currentUser;
    private Calendar regDate;
    private Boolean setCurrentDate;
    private String action;

    public UserBean() {
        name = "n";
        info = "i";
        login = "uog";
        password = "p";
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        if(currentUser == null) return null;
        return currentUser.getName();
    }

    public void setName(String name) {
        currentUser.setName(name);
    }

    public String getInfo() {
        if(currentUser == null) return null;
        return currentUser.getInfo();
    }

    public void setInfo(String info) {
        currentUser.setInfo(info);
    }

    public String getLogin() {
        if(currentUser == null || currentUser.getAuthorizationInfo() == null)
            return null;
        return currentUser.getAuthorizationInfo().getLogin();
    }

    public void setLogin(String login) {
        if(currentUser.getAuthorizationInfo() == null) {
            AuthorizationInfo authInfo = new Auth
        }
        currentUser.getAuthorizationInfo().setLogin(login);
    }

    public String getPassword() {
        if(currentUser == null || currentUser.getAuthorizationInfo() == null)
            return null;
        return currentUser.getAuthorizationInfo().getPassword();
    }

    public void setPassword(String password) {
        currentUser.getAuthorizationInfo().setPassword(password);
    }

    public Date getRegistrationDate() {
        if(currentUser == null) return null;
        return currentUser.getRegistrationDate();
    }

    public void setRegistrationDate(Date date) {
        currentUser.setRegistrationDate(date);
    }
/*
    public String getYear() {
        if(currentUser == null) return "";
        return String.valueOf(regDate.get(Calendar.YEAR));
    }

    public void setYear(String year) {
        this.year = Integer.parseInt(year);
    }

    public String getMonth() {
        if(currentUser == null) return "";
        return String.valueOf(regDate.get(Calendar.MONTH));
    }

    public void setMonth(String month) {
        this.month = Integer.parseInt(month);
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }

    public Integer getHours() {
        return hours;
    }

    public void setHours(Integer hours) {
        this.hours = hours;
    }

    public Integer getMinutes() {
        return minutes;
    }

    public void setMinutes(Integer minutes) {
        this.minutes = minutes;
    }

    public Integer getSeconds() {
        return seconds;
    }

    public void setSeconds(Integer seconds) {
        this.seconds = seconds;
    }*/

    private String getSearchParameter() {
        return "";
    }

    public void setSearchParameter(String searchParameter) {
        this.searchParameter = searchParameter;
    }

    public Map<String, Integer> getAllUsers() throws DataAccessException {
        Collection<User> users = dataManager.getAllUsers();
        Map<String, Integer> allUsers = new LinkedHashMap<String, Integer>();
        
        for(User user: users) {
            String description = String.format("%d. %s", user.getId(), user.getName());
            allUsers.put(description, user.getId());
        }
        if(allUsers.size() < 1) {
            currentUser = null;
        }

        return allUsers;
    }

    public Integer getSelectedUserId() {
        return selectedUserId;
    }

    public void setSelectedUserId(Integer selectedUserId) {
        this.selectedUserId = selectedUserId;
    }

    public Boolean getSetCurrentDate() {
        return setCurrentDate;
    }

    public void setSetCurrentDate(Boolean setCurrentDate) {
        this.setCurrentDate = setCurrentDate;
    }

    public void search(String param) {
        System.out.println("searching...");
    }

    public String getAction() {
        //System.out.println("current user: "+currentUser);
        return action;
    }

    public void doAction() throws DataAccessException {
        if(action.equals(ADD)) {
            add();
        } else {
            edit();
        }
        action = VIEW;
    }

    public void add() throws DataAccessException {
        dataManager.addUser(currentUser);
    }

    public void edit() throws DataAccessException {
        dataManager.replaceUser(currentUser);
    }

    public void delete() throws DataAccessException {
        System.out.println("on click: "+selectedUserId+", inf: "+info);
        dataManager.deleteUserById(selectedUserId);
        currentUser = null;
    }

    public void setCurrentUser() throws DataAccessException {
        currentUser = dataManager.getUserById(selectedUserId);
    }

    public void ensureCurrentUserSet() throws DataAccessException {
        if(currentUser == null) {
            action = NO_USER_TO_EDIT;
        } else {
            //work around-ец
            if(currentUser.getAuthorizationInfo() == null) {
                currentUser = dataManager.getUserById(selectedUserId);
                if(currentUser == null) {
                    action = NO_USER_TO_EDIT;
                } else {
                    setCurrentDate = false;
                    action = EDIT;
                }
            } else {
                action = EDIT;
            }
        }
        System.out.println("action: "+action);
    }

    public void unsetCurrentUser() {
        currentUser = new User();
        setCurrentDate = true;
        action = ADD;
    }
}